package com.xinchuang.service.targetSystem.Impl;

import cn.hutool.core.util.ObjectUtil;
import com.xinchuang.common.Result;
import com.xinchuang.common.ResultUtil;
import com.xinchuang.mapper.targetSystem.SystemMasterMapper;
import com.xinchuang.mapper.targetSystem.SystemSortMapper;
import com.xinchuang.mapper.targetSystem.SystemsMasterMapper;
import com.xinchuang.entity.targetSystem.system.SystemMaster;
import com.xinchuang.entity.targetSystem.system.SystemsMaster;
import com.xinchuang.mapper.targetSystem.SystemMasterMapper;
import com.xinchuang.service.targetSystem.SystemSortService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @ClassName SystemSortServiceImpl
 * @Description: TODO(写一段描述的话)
 * @Author zhangxuan
 * @Date 2023-08-07 17:54
 * @Verion V1.0
 */
@Service
@RequiredArgsConstructor
public class SystemSortServiceImpl implements SystemSortService {

    private final SystemSortMapper systemSortMapper;

    private final SystemMasterMapper systemMasterMapper;

    private final SystemsMasterMapper systemsMasterMapper;

    @Override
    public Result getSort(String id, Integer type, Integer status) {
        if (type == 1) {
            SystemMaster systemMaster = systemMasterMapper.selectSystemOnly(id);
            if (ObjectUtil.isNull(systemMaster)) {

            }
            SystemMaster system;
            if (status == 1 || status == 2) {
                system = systemSortMapper.getSystemLT(systemMaster.getSort());
                if (ObjectUtil.isNull(system)) {
                    return ResultUtil.error(400, "已经置顶，请勿重复操作！");
                }
            } else {
                system = systemSortMapper.getSystemGT(systemMaster.getSort());
                if (ObjectUtil.isNull(system)) {
                    return ResultUtil.error(400, "已经置底，请勿重复操作！");
                }
            }
            if (status == 1) {
                Integer min = systemSortMapper.getSystemMin();
                systemSortMapper.updateSystem(id, min - 1);
            } else if (status == 2 || status == 3) {
                systemSortMapper.updateSystem(systemMaster.getSId(), system.getSort());
                systemSortMapper.updateSystem(system.getSId(), systemMaster.getSort());
            } else if (status == 4){
                Integer max = systemSortMapper.getSystemMax();
                systemSortMapper.updateSystem(id, max + 1);
            }
        } else if (type == 2) {
            SystemsMaster systemsMaster = systemsMasterMapper.selectSystemOnlyBySId(id);
            if (ObjectUtil.isNull(systemsMaster)) {

            }
            SystemsMaster system;
            if (status == 1 || status == 2) {
                system = systemSortMapper.getSystemsLT(systemsMaster.getSort());
                if (ObjectUtil.isNull(system)) {
                    return ResultUtil.error(400, "已经置顶，请勿重复操作！");
                }
            } else {
                system = systemSortMapper.getSystemsGT(systemsMaster.getSort());
                if (ObjectUtil.isNull(system)) {
                    return ResultUtil.error(400, "已经置底，请勿重复操作！");
                }
            }
            if (status == 1) {
                Integer min = systemSortMapper.getSystemsMin();
                systemSortMapper.updateSystems(id, min - 1);
            } else if (status == 2 || status == 3) {
                systemSortMapper.updateSystems(systemsMaster.getSysId(), system.getSort());
                systemSortMapper.updateSystems(system.getSysId(), systemsMaster.getSort());
            } else if (status == 4){
                Integer max = systemSortMapper.getSystemsMax();
                systemSortMapper.updateSystems(id, max + 1);
            }
        }
        return ResultUtil.success();
    }

}